﻿

using IotClientService.Models.Lg;
using IotClientService.Services;
using IotContract.Enums;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XT.Common.Extensions;

namespace IotClientService.Handles.lgs;
/// <summary>
/// 火灾上位机
/// </summary>
public class LgHandleFire:LgHandleBase
{
    public LgHandleFire(ILgApiService lgCallApiService) : base(lgCallApiService)
    {
    }

    /// <summary>
    /// 设备类型
    /// </summary>
    public override IotEquipType EquipType { get; set; } = IotEquipType.FIRE;
    /// <summary>
    /// 处理消息
    /// </summary>
    /// <param name="msg"></param>
    /// <returns></returns>
    /// <exception cref="NotImplementedException"></exception>
    public override async Task<bool> HandleMsg(LgMsgInfo msg)
    {
        string replyCmd = string.Empty;
        switch (msg.Command)
        {
            // 报告有无火灾
            case "901":
                replyCmd = "902";
                Handle901(msg, replyCmd);
                break;

            // 设备状态报告指示回应
            case "910":
               await Handle910(msg, "910");
                // send 833 

                break;
           
            
            // 发生火灾，报告设备
            case "933":
                replyCmd = "934";
               await Handle933(msg, replyCmd);
                break;



            // 设置时间指示
            case "999":
               await Handle999(msg, "999");
                break;
            default:
              
                break;
        }
        return true;
    }
    /// <summary>
    /// 上位机报告复位
    /// </summary>
    /// <param name="msg"></param>
    /// <param name="replyCmd"></param>
    private async void Handle901(LgMsgInfo msg, string replyCmd)
    {
        var sendMsg = new LgMsgInfo
        {
            Direction = 0,
            ObjID = msg.SeqNo,
            HasMsg = 0,
            SeqNo = msg.SeqNo,
            Command = replyCmd,

        };

    
    // 工程代码  成品库:2  常温:3  高温:4
    var code = msg.Message.Substring(18, 1);
    var num = msg.Message.Substring(19, 6);


    var result=  await Api.CommitFire(new Models.Lg.Remote.LgFireRequest
    {
        EquipNum=Equip.Num,
        EquipType = this.EquipType,
        ProgramType = Equip.ProgramType,
        Type = 0,
        PositionCode = code,
        LocationNum = num
    });

    Equip.Remark = $"fire reset:{code} num:{num}";
    if (!result.Success)
    {
        Equip.ExMessage = result.Message;
        return;
    }
    #region 传输body

      
    // 0:ok 1:ng 
    int ng = result.Result.Ret;
    var ngCode = result.Result.NgCodes?.FirstOrDefault() ?? "0000";




    #endregion
    msg.Body = $"{ng}{ngCode}";
    msg.IsSend = true;
    InvokeSend(sendMsg);


}


    
  
  
   
    /// <summary>
    /// 发生火灾，上位机报告FMS
    /// </summary>
    /// <param name="msg"></param>
    /// <param name="replyCmd"></param>
    private async Task Handle933(LgMsgInfo msg, string replyCmd)
    {
        var sendMsg = new LgMsgInfo
        {
            Direction = 0,
            ObjID = msg.SeqNo,
            HasMsg = 0,
            SeqNo = msg.SeqNo,
            Command = replyCmd,

        };
        // 工程代码  成品库:2  常温:3  高温:4
        var code = msg.Message.Substring(18, 1);
        var num = msg.Message.Substring(19, 6);

        var result = await Api.CommitFire(new Models.Lg.Remote.LgFireRequest
        {
            EquipNum=Equip.Num,
            EquipType = this.EquipType,
            ProgramType = Equip.ProgramType,
            Type = 1,
            PositionCode = code,
            LocationNum = num
        });

        Equip.Remark = $"fire error:{code} num:{num}";

        #region 传输body

      
        // 0:ok 1:ng 
        int ng = result.Result.Ret;
        var ngCode = result.Result.NgCodes?.FirstOrDefault() ?? "0000";




        #endregion
        msg.Body = $"{ng}{ngCode}";
        msg.IsSend = true;
        InvokeSend(sendMsg);
    }

   


  
}
